//
//  LoginView.m
//  SHHCPhone
//
//  Created by sheng long on 14-6-17.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "LoginView.h"
#import "TextFieldView.h"

@interface LoginView ()<UITextFieldDelegate>
{
    UIScrollView *mainScroll;
    
    TextFieldView *field1;
    TextFieldView *field2;
}
@end

@implementation LoginView

@synthesize delegate;

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginViewMsgIsNil) name:LOGINVIEW_MSG_NULL object:nil];
        
        mainScroll = [[UIScrollView alloc] initWithFrame:self.bounds];
        [mainScroll setBackgroundColor:[UIColor clearColor]];
        [self addSubview:mainScroll];
        [mainScroll setBounces:NO];
        [mainScroll setShowsHorizontalScrollIndicator:NO];
        [mainScroll setShowsVerticalScrollIndicator:NO];
        
        UIButton *bgButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [bgButton setFrame:mainScroll.bounds];
        [mainScroll addSubview:bgButton];
        [bgButton addTarget:self action:@selector(bgButt) forControlEvents:UIControlEventTouchUpInside];
        
        UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"登陆页-用户默认头像.png"]];
        [image sizeToFit];
        [mainScroll addSubview:image];
        [image setCenter:CGPointMake(self.frame.size.width/2, 16+image.frame.size.height/2)];
        
        field1 = [[TextFieldView alloc] init];
        [field1 setBgImage:@"输入框.png" titleImage:@"登录页-手机icon.png" tip:@"输入手机号"];
        [mainScroll addSubview:field1];
        [field1.textField setDelegate:self];
        [field1 setCenter:CGPointMake(image.center.x, image.frame.size.height+image.frame.origin.y+39+field1.frame.size.height/2)];
        field1.textField.keyboardType = UIKeyboardTypeNumberPad;
        
        field2 = [[TextFieldView alloc] init];
        [field2 setBgImage:@"输入框.png" titleImage:@"登录页-密码icon.png" tip:@"输入密码"];
        [mainScroll addSubview:field2];
        [field2.textField setDelegate:self];
        [field2 setCenter:CGPointMake(field1.center.x, field1.frame.size.height+field1.frame.origin.y+19+field2.frame.size.height/2)];
        field2.textField.secureTextEntry = YES;
        field2.textField.returnKeyType = UIReturnKeyDone;
        [field2.textField addTarget:self action:@selector(bgButt) forControlEvents:UIControlEventEditingDidEndOnExit];

        
        UIButton *button1 = [OftenMenthod setButtonTitle:@"登录"];
        [mainScroll addSubview:button1];
        [button1 addTarget:self action:@selector(loginButt) forControlEvents:UIControlEventTouchUpInside];
        [button1 setCenter:CGPointMake(field2.center.x, field2.frame.size.height+field2.frame.origin.y+45+button1.frame.size.height/2)];
        
        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, button1.frame.size.height+button1.frame.origin.y+30, self.frame.size.width, 13)];
        [label1 setText:@"忘记密码"];
        [label1 setTextColor:GET_COLOR(@"3e3e3e")];
        [label1 setBackgroundColor:[UIColor clearColor]];
        [mainScroll addSubview:label1];
        [label1 setTextAlignment:NSTextAlignmentCenter];
        [label1 setFont:[UIFont boldSystemFontOfSize:12]];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame:label1.frame];
        [mainScroll addSubview:button];
        [button setShowsTouchWhenHighlighted:YES];
        [button addTarget:self action:@selector(passButt) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *label2 = [[UILabel alloc] init];
        [label2 setText:@"or"];
        [label2 setTextColor:GET_COLOR(@"b8b8b8")];
        [label2 setBackgroundColor:[UIColor whiteColor]];
        [mainScroll addSubview:label2];
        [label2 setTextAlignment:NSTextAlignmentCenter];
        [label2 setFont:[UIFont boldSystemFontOfSize:14]];
        [label2 sizeToFit];
        [label2 setCenter:CGPointMake(self.frame.size.width/2, label1.frame.size.height+label1.frame.origin.y+30+label2.frame.size.height/2)];
        
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, label2.frame.origin.x-5, 1)];
        [view1 setBackgroundColor:GET_COLOR(@"ebebeb")];
        [view1 setCenter:CGPointMake(view1.frame.size.width/2, label2.center.y+1)];
        [view1 setAlpha:.8];
        [mainScroll addSubview:view1];
        
        UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, label2.frame.origin.x-5, 1)];
        [view2 setBackgroundColor:GET_COLOR(@"ebebeb")];
        [view2 setCenter:CGPointMake(mainScroll.frame.size.width-view1.frame.size.width/2, view1.center.y)];
        [view2 setAlpha:.8];
        [mainScroll addSubview:view2];
        
        UIButton *button2 = [OftenMenthod setRegisterButtonTitle:@"立即注册"];
        [mainScroll addSubview:button2];
        [button2 setCenter:CGPointMake(field2.center.x, label2.frame.size.height+label2.frame.origin.y+37+button2.frame.size.height/2)];
        [button2 addTarget:self action:@selector(registerButt) forControlEvents:UIControlEventTouchUpInside];
        
        [mainScroll setContentSize:CGSizeMake(0, button2.frame.size.height+button2.frame.origin.y+10)];
    }
    return self;
}

-(void)loginViewMsgIsNil
{
    [field1.textField setText:nil];
    [field2.textField setText:nil];
}

-(void)bgButt
{
    [field1.textField resignFirstResponder];
    [field2.textField resignFirstResponder];
}

-(void)loginButt
{
//    field1.textField.text = @"18201122110";
//    field2.textField.text = @"111111";
    
    if (field1.textField.text.length != 11) {
        [LoadingFrameView autoDisappearTipview:@"请输入11位数的手机号码"];
    }else if (field2.textField.text.length==0){
        [LoadingFrameView autoDisappearTipview:@"请输入密码"];
    }else{
        [self bgButt];
    
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:1];
        [dic setValue:field1.textField.text forKey:@"phone"];
        [dic setValue:field2.textField.text forKey:@"password"];
        [dic setValue:IOS_LOGIN forKey:IOS_METHOD_NAME];
        
        if ([(NSObject*)self.delegate respondsToSelector:@selector(login:)]) {
            [self.delegate login:dic];
        }
    }
}

-(void)passButt
{
    if ([(NSObject*)self.delegate respondsToSelector:@selector(pass)]) {
        [self.delegate pass];
    }
}

-(void)registerButt
{
    if ([(NSObject*)self.delegate respondsToSelector:@selector(registerUser)]) {
        [self.delegate registerUser];
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if ([textField isEqual:field2.textField]) {
        CGFloat height = CURRENT_CONTENT_HEIGHT-(field2.frame.origin.y+field2.frame.size.height-mainScroll.contentOffset.y+NAVIGATIONBAR_HEIGHT);
        if (height < 216) {
            [mainScroll setContentOffset:CGPointMake(0, field2.frame.origin.y+field2.frame.size.height-(CURRENT_CONTENT_HEIGHT-216-NAVIGATIONBAR_HEIGHT))];
        }
    }
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //最大字符个数
    NSInteger MAX_CHARS = 30;
    if ([textField isEqual:field1.textField]) {
        MAX_CHARS = 11;
    }
    
    NSMutableString *newtxt = [NSMutableString stringWithString:textField.text];
    [newtxt replaceCharactersInRange:range withString:string];
    
    return ([newtxt length] <= MAX_CHARS);
}

@end
